package com.guidance.guidance.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.guidance.guidance.domain.Message;
import com.guidance.guidance.domain.User;
import com.guidance.guidance.repository.UserRepository;

@RestController
@RequestMapping("/allNumber")
public class AllNumberController {

	@Autowired
	private UserRepository userRepository;
	
	/**
	 * 获得所有的成员
	 * @param msg
	 * @return
	 */
	@RequestMapping("/getAllNumber")
	public Message<List> getAllNumber(Message<List> msg){
		List<User> userList = userRepository.findAll();
		if(userList!=null&&userList.size()!=0) {
			msg.setCode("200");
			msg.setMsg("获取成员成功");
			msg.setSuccess("true");
			msg.setData(userList);
		}else {
			msg.setCode("404");
			msg.setMsg("获取成员失败");
			msg.setSuccess("false");		
		}
		return msg;
	}
	
	/**
	 * 删除某一位的成员，使用时参数该成员的telephone
	 * @param msg
	 * @param u
	 * @return
	 */
	@RequestMapping("/removeNumber")
	public Message<String> removeNumber(Message<String> msg,User u){
		User user = userRepository.findByTelephone(u.getTelephone());
		if(user != null) {
			userRepository.delete(user);
			msg.setCode("200");
			msg.setMsg("删除成功");
			msg.setSuccess("true");
		}else {
			msg.setCode("404");
			msg.setMsg("操作失败");
			msg.setSuccess("false");
		}
		return msg;
	}
}
